﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Net.Chirash.Twitter.TwitterClient;
using Net.Chirash.Twitter.TwitterDatabase;

namespace Net.Chirash.Twitter.TweetZone.WindowComponents.ViewModels
{
    public class UserContainerViewModel : Net.Chirash.Twitter.TweetZone.WindowComponents.ViewModels.PagableContainerViewModel<User>
    {
        #region Constructors

        //public UserContainerViewModel(UserListType type)
        //    : this(type, App.CurrentApp.User)
        //{
        //}

        public UserContainerViewModel(UserListType type, User currentUser)
            : base()
        {
            this.ListType = type;
            this.CurrentUser = currentUser;
            this.TwitterCommand = TwitterCommandFactory.CreateRemoteTwitterCommand();
        }

        #endregion

        #region Commands

        public ICommand OpenUserProfileWindow
        {
            get
            {
                return new RelayCommand(new Action<object>((param) => { var wnd = new UserProfileWindow(param as User, true); }),
                                         new Predicate<object>((param) => param != null && param is User));
            }
        }

        #endregion

        #region Public Properties

        #region public UserListType ListType { get; set; }
        private UserListType _ListType;
        public UserListType ListType
        {
            get { return _ListType; }
            set { _ListType = value; OnPropertyChanged("ListType"); }
        }
        #endregion

        public override string ContainerTitle
        {
            get
            {
                var ret = Enum.GetName(typeof(UserListType), this.ListType);
                if (this.CurrentUser != null)
                    ret += string.Format(" of @{0}", this.CurrentUser.ScreenName);
                return ret;
            }
        }

        #endregion

        #region Protected Members

        protected class UserRefreshArgs
            : PagingEventArgs<User>
        {
            public UserListType type;
        }

        protected override void UpdateDatabase(TwitterCaching caching, IEnumerable<User> updatelist)
        {
            caching.UpdateUsers(updatelist);
            switch (this.ListType)
            {
                case UserListType.Friend:
                    caching.UpdateUserFriends(this.CurrentUser.UserID, updatelist);
                    break;
                case UserListType.Follower :
                    caching.UpdateUserFollowers(this.CurrentUser.UserID, updatelist);
                    break;
            }
        }

        protected override RefreshArgs<User> CreateRefreshArgs(RefreshArgs<User> args)
        {
            UserRefreshArgs result = args as UserRefreshArgs;
            if (args == null)
                result = new UserRefreshArgs();

            base.CreateRefreshArgs(result);
            result.type = this.ListType;

            return result;
        }

        protected override void RefreshDataProviding(RefreshArgs<User> e)
        {
            var args = e as UserRefreshArgs;
            var option = new UserListOption()
            {
                User = args.CurrentUser,
                Cursor = -1,
            };

            var list = TwitterDataFactory.GetUserList(option, args.type, args.TwitterCommand);
            if (e.CollectionCount == 0)
                this.nextCursor = list.NextCursor;

            args.ResultEnumerable = list;
        }

        protected override void PagingDataProviding(PagingEventArgs<User> e)
        {
            var args = e as UserRefreshArgs;
            var option = new UserListOption()
            {
                User = args.CurrentUser,
                Cursor = this.nextCursor,
            };

            var list = TwitterDataFactory.GetUserList(option, args.type, args.TwitterCommand);
            this.nextCursor = list.NextCursor;
            args.ResultEnumerable = list;
        }

        #endregion

        #region Private Members

        private long nextCursor;

        #endregion
    }

}
